package com.insist.util;

import com.insist.entity.UserInfo;
import com.insist.enums.CmmNoType;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Administrator on 2016/6/28.
 */
public class CmmUtils {
    public static Logger logger = Logger.getLogger(CmmUtils.class);
    public final static Boolean IS_DEV = false;
    public final static String baseUrl = "http://1.insist.cn";

    public final static String chargeReturnUrl ="http://1.insist.cn/order.do";

    public final static String keyId = "c2f38bc5-ae7c-4e69-a83e-e2e8f1c8a6fc";

    public final static String keySecret= "e2f2173b-42e9-46d9-bccc-82fa9806a73d";


    public static String getValidCode(int length){
        Random random = new Random();
        String validCode= "";
        for(int i = 0;i<length;i++){
            validCode+=random.nextInt(10);
        }
        return validCode;
    }

    public static UserInfo getLoginUser(){
        Subject currentUser = SecurityUtils.getSubject();
        Long id = (Long)currentUser.getPrincipal();
        if(id == null){
            return null;
        }
        UserInfo user = ServiceUtil.getUserService().getById(id);
        return user;
    }

    public static Long getLoginUserId(){
        Subject currentUser = SecurityUtils.getSubject();
        return (Long)currentUser.getPrincipal();
    }

    private static AtomicInteger ORDER_NO_GENERATOR = new AtomicInteger(1);
    private static Map<Integer,AtomicInteger> ORDER_NO_GENERATOR_MAP = new ConcurrentHashMap<>();
    private static Map<Integer,String> ORDER_NO_DATE_MAP = new ConcurrentHashMap<>();
    private static final DateFormat df = new SimpleDateFormat("yyyyMMdd");

    public static String createOrderNo(CmmNoType type) {
        if(ORDER_NO_DATE_MAP.get(type.getType())==null){
            String lastNo = findFromDb(type);
            if(StringUtils.isNotEmpty(lastNo)){

                String date = lastNo.substring(0,8);
                String index = lastNo.substring(9,lastNo.length());
                logger.info(lastNo+"_"+date+"_"+index);
                Integer ind = Integer.valueOf(index);
                ORDER_NO_DATE_MAP.put(type.getType(),date);
                ORDER_NO_GENERATOR_MAP.put(type.getType(),new AtomicInteger(ind));
            }
        }

        String nowDate = df.format(new Date());
        if(nowDate.equals(ORDER_NO_DATE_MAP.get(type.getType()))){
            AtomicInteger integer = ORDER_NO_GENERATOR_MAP.get(type.getType());
            Integer num = integer.addAndGet(1);
            return nowDate+(type.getType()*100000+num);
        } else {
            ORDER_NO_DATE_MAP.put(type.getType(),nowDate);
            AtomicInteger integer = new AtomicInteger(0);
            ORDER_NO_GENERATOR_MAP.put(type.getType(),integer);
            Integer num = integer.addAndGet(1);
            return nowDate+(type.getType()*100000+num);
        }
    }

    private static String findFromDb(CmmNoType type) {
        switch (type){
            case CHARGE:
                return ServiceUtil.getChargeService().getMaxOrderNo();
            case ORDER:
                return ServiceUtil.getOrderService().getMaxOrderNo();
            case PRODUCT:
                return ServiceUtil.getProductService().getMaxProductNo();
        }
        return null;
    }

    public static String getHideMobile(String mobile){
        if(mobile==null||mobile.length()< 11){
            return "";
        }
        return mobile.substring(0,3)+"****"+mobile.substring(7,11);
    }

    public static String replacePictureUrl(String images){
        if(StringUtils.isEmpty(images)){
            return "";
        }
        List<String > pictureUrls = new ArrayList<>();
        for(String pictureId:images.split("\\|")){
            if(StringUtils.isNotEmpty(pictureId)){
                pictureUrls.add("http://insist.oss-cn-hangzhou.aliyuncs.com//"+pictureId);
            }
        }
        return StringUtils.join(pictureUrls,"|");
    }

//    public static void main(String[] args) {
//        System.out.println(getHideMobile("13018093833"));
//    }
    public static void main(String[] args) {
        System.out.println("【insist】您的注册短信验证码为12345,30分钟内输入有效");
    }
}
